package com.example.user.controller;

import com.example.user.constants.AuthUserConstant;
import com.example.user.service.AuthUserService;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUserRealname;
import com.usian.utils.common.FileUtils;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Fastdfscontroller
 *
 */
@RestController
@Slf4j
public class FastDFSController {
    @Autowired
    AuthUserService authUserService;
    @Autowired
    private FastDFSClient fastDFSClient;
    @Value("${fastdfs.url}")
    private String url;
    
    
    /**
     * 上传img
     *
     * @param file 文件
     * @return {@code ResponseResult}
     * @throws IOException ioexception
     */
    @PostMapping("/upload_img")
    public ResponseResult uploadImg(MultipartFile file) throws IOException {
        if (file==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    
        }
        String imgPath=fastDFSClient.uploadFile(file);
        
        return authUserService.saveImg(imgPath);
    }
    
    
    /**
     * 下载img
     *
     * @return {@code ResponseResult}
     * @throws IOException ioexception
     */
    @PostMapping("/download_img")
    public ResponseResult downloadImg(Integer id) throws IOException {
        ApUserRealname userById = authUserService.getUserById(id);
        if (userById==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        String backImage = userById.getBackImage();
        if ("".equals(backImage)||backImage==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        int i = backImage.indexOf("/");
        String imgPath = backImage.substring(i + 1);
    
        byte[] bytes = fastDFSClient.download("group1", imgPath);
        long imgName = System.currentTimeMillis();
        String path="e:\\img\\"+imgName+ AuthUserConstant.SUFFIX_JPG;
        log.info("保存文件路径"+path);
        FileUtils.writeBytesToFileClass(bytes,path);
        return ResponseResult.okResult("下载成功");
    }
    
    
    /**
     * 删除img
     *
     * @param id id
     * @return {@code ResponseResult}
     * @throws IOException ioexception
     */
    @PostMapping("/del_img")
    public ResponseResult delImg(Integer id) throws IOException {
    
        ApUserRealname apUserRealname = authUserService.getUserById(id);
        if (apUserRealname==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        String backImage = apUserRealname.getBackImage();
        if (apUserRealname.getBackImage()==null||"".equals(backImage)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        apUserRealname.setBackImage("");
        Integer update = authUserService.update(apUserRealname);
        if (update!=1){
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
        fastDFSClient.delFile(backImage);
        return ResponseResult.okResult("删除成功");
    }
}
